import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import pojo.SeatInfo;

import java.io.*;
import java.util.List;

public class getAllSeatInfo {
    public static void main(String[] args) throws IOException {
//        BufferedWriter bw = new BufferedWriter(new FileWriter( "E:\\libraryInfos\\FourthNorth.txt"));
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        //url链接更改教室id以及日期即可预约
        HttpGet httpGet = new HttpGet("http://10.21.95.57/ClientWeb/pro/ajax/device.aspx?byType=devcls&classkind=8&display=fp&md=d&room_id=100598302&purpose=&selectOpenAty=&cld_name=default&date=2020-09-23&fr_start=16%3A50&fr_end=17%3A50&act=get_rsv_sta&_=1600851046351");
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();


//                responseEntity.writeTo(byteArrayOutputStream);

                String json = EntityUtils.toString(responseEntity);

                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(json);

                String data = jsonObject.getString("data");



                List<SeatInfo> seatInfos = JSON.parseArray(data, SeatInfo.class);

                CSVPrinter seatInfoCsv = initCsv("BoXueSeatInfos");

                ;


                for (SeatInfo seatInfo : seatInfos) {
                    seatInfoCsv.printRecord(seatInfo.getTitle(),seatInfo.getDevId());
                    System.out.println(seatInfo);
                }

                seatInfoCsv.flush();
                seatInfoCsv.close();


//                System.out.println("响应内容为:" + json);


            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }






    public static CSVPrinter initCsv(String seatPosition) throws IOException {
        FileOutputStream fos = new FileOutputStream("E:\\libraryInfos\\"+seatPosition+".csv",true);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");

        CSVFormat csvFormat = CSVFormat.DEFAULT;


        CSVPrinter csvPrinter = new CSVPrinter(osw, csvFormat);
//        csvPrinter.printRecord("jj","mmmm");
        return csvPrinter;

    }
}

